import wepy from 'wepy'
import { apiPrefix } from '@/config'

export default async function api (config) {
  if (config.url[0] === '/') {
    config.url = `${apiPrefix}${config.url}`
    if (!config.header) {
      config.header = {}
    }
    // debugger
    // config.header['Authorization'] = `Bearer ${getApp().globalData.token}`
  }
  await wepy.showLoading({
    title: '加载中...',
    mask: true
  })
  let rz = await wepy.request(config)
  await wepy.hideLoading()
  if (rz.statusCode === 200) {
    return rz.data
  }
  wepy.showModal({
    title: '提示',
    content: rz.data.message || rz.data,
    showCancel: false,
    confirmText: '知道了',
    confirmColor: '#ff6369'
  })
  return {
    err: rz.data,
    status: rz.statusCode
  }
}
